package Simple.SearchTwoNum;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * 题目要求：
 * 给定一个整数数组 nums 和一个整数目标值 target，
 * 请你在该数组中找出 和为目标值 target的那两整数，并返回它们的数组下标。
 */

public class searchTwoNum1 {
    public static void main(String[] args) {
        int[] nums = new int[]{-3,4,3,90};
        int target = 0;
        System.out.println(Arrays.toString(twoSum(nums, target)));
    }
    public static int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        ArrayList<Integer> canSelect = new ArrayList<Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (canSelect.contains(target - nums[i])) {
                result[0] = searchIndex(nums, target - nums[i], i);
                result[1] = i;
                return result;
            }else {
                canSelect.add(nums[i]);
            }
        }
        return result;
    }

    public static int searchIndex(int[] nums,int target, int currentIndex) {
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == target && i != currentIndex) {
                return i;
            }
        }
        return -1;
    }
}